﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using UnityEngine;

    internal class ObjectBrowserTreeViewGUI : DefaultTreeViewGUI
    {
        private const float k_DistBetweenRootTypes = 15f;
        private Texture2D k_FavoriteFilterIcon;
        private Texture2D k_FavoriteFolderIcon;
        private Texture2D k_FavoritesIcon;
        private bool m_IsCreatingSavedFilter;

        public ObjectBrowserTreeViewGUI(TreeView treeView) : base(treeView)
        {
            this.k_FavoritesIcon = EditorGUIUtility.FindTexture("Favorite Icon");
            this.k_FavoriteFolderIcon = EditorGUIUtility.FindTexture("FolderFavorite Icon");
            this.k_FavoriteFilterIcon = EditorGUIUtility.FindTexture("Search Icon");
        }

        internal virtual void BeginCreateSavedFilter(SearchFilter filter)
        {
            string displayName = "New Saved Search";
            this.m_IsCreatingSavedFilter = true;
            int instanceID = SavedSearchFilters.AddSavedFilter(displayName, filter, GetListAreaGridSize());
            base.m_TreeView.Frame(instanceID, true, false);
            base.m_TreeView.m_State.m_RenameOverlay.BeginRename(displayName, instanceID, 0f);
        }

        public override void GetFirstAndLastRowVisible(List<TreeView.Node> rows, float topPixel, float heightInPixels, out int firstRowVisible, out int lastRowVisible)
        {
            firstRowVisible = (int) Mathf.Floor(topPixel / 16f);
            lastRowVisible = firstRowVisible + ((int) Mathf.Ceil(heightInPixels / 16f));
            float num = 0.9375f;
            firstRowVisible -= (int) Mathf.Ceil(2f * num);
            lastRowVisible += (int) Mathf.Ceil(2f * num);
            firstRowVisible = Mathf.Max(firstRowVisible, 0);
            lastRowVisible = Mathf.Min(lastRowVisible, rows.Count - 1);
        }

        public override float GetHeightOfLastRow()
        {
            return 16f;
        }

        protected override Texture GetIconForNode(TreeView.Node node)
        {
            if ((node != null) && (node.icon != null))
            {
                return node.icon;
            }
            SearchFilterTreeNode node2 = node as SearchFilterTreeNode;
            if (node2 == null)
            {
                return base.GetIconForNode(node);
            }
            if (this.IsVisibleRootNode(node))
            {
                return this.k_FavoritesIcon;
            }
            if (node2.isFolder)
            {
                return this.k_FavoriteFolderIcon;
            }
            return this.k_FavoriteFilterIcon;
        }

        public static float GetListAreaGridSize()
        {
            float listAreaGridSize = -1f;
            if (ObjectBrowser.s_LastInteractedObjectBrowser != null)
            {
                listAreaGridSize = ObjectBrowser.s_LastInteractedObjectBrowser.listAreaGridSize;
            }
            return listAreaGridSize;
        }

        public override int GetNumRowsOnPageUpDown(TreeView.Node fromNode, bool pageUp, float heightOfTreeView)
        {
            return (((int) Mathf.Floor(heightOfTreeView / 16f)) - 1);
        }

        public override float GetTopPixelOfRow(int row, List<TreeView.Node> rows)
        {
            float num = row * 16f;
            TreeView.Node node = rows[row];
            if (ObjectBrowser.GetItemType(node.instanceID) == ObjectBrowser.ItemType.Asset)
            {
                num += 15f;
            }
            return num;
        }

        public override Vector2 GetTotalSize(List<TreeView.Node> rows)
        {
            Vector2 totalSize = base.GetTotalSize(rows);
            totalSize.y += 15f;
            return totalSize;
        }

        private bool IsVisibleRootNode(TreeView.Node node)
        {
            return (base.m_TreeView.m_Data as ObjectBrowserTreeViewDataSource).IsVisibleRootNode(node);
        }

        public override Rect OnRowGUI(TreeView.Node node, int row, float rowWidth, bool selected, bool focused)
        {
            float top = row * 16f;
            if (ObjectBrowser.GetItemType(node.instanceID) == ObjectBrowser.ItemType.Asset)
            {
                top += 15f;
            }
            Rect rect = new Rect(0f, top, rowWidth, 16f);
            bool useBoldFont = this.IsVisibleRootNode(node);
            base.DoNodeGUI(rect, node, selected, focused, useBoldFont);
            return rect;
        }

        protected override void RenameEnded()
        {
            int userData = base.GetRenameOverlay().userData;
            ObjectBrowser.ItemType itemType = ObjectBrowser.GetItemType(userData);
            if (this.m_IsCreatingSavedFilter)
            {
                this.m_IsCreatingSavedFilter = false;
                if (base.GetRenameOverlay().userAcceptedRename)
                {
                    SavedSearchFilters.SetName(userData, base.GetRenameOverlay().name);
                    int[] selectedInstanceIDs = new int[] { userData };
                    base.m_TreeView.SetSelection(selectedInstanceIDs, true);
                }
                else
                {
                    SavedSearchFilters.RemoveSavedFilter(userData);
                }
            }
            else if (itemType == ObjectBrowser.ItemType.SavedFilter)
            {
                if (base.GetRenameOverlay().userAcceptedRename)
                {
                    SavedSearchFilters.SetName(userData, base.GetRenameOverlay().name);
                }
            }
            else
            {
                base.RenameEnded();
                if (base.GetRenameOverlay().userAcceptedRename)
                {
                    base.m_TreeView.NotifyListenersThatSelectionChanged();
                }
            }
        }
    }
}

